package com.example.netty.service;

import com.alibaba.fastjson.JSON;
import com.example.netty.common.Message;
import com.example.netty.constant.ResMessageType;
import com.example.netty.vo.Room;
import com.example.netty.vo.User;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 房间服务类
 * todo:自动检测空房间,并删除
 * @author zclever <304078606@qq.com>
 * @date 2021-03-02
 */
@Component
public class RoomService {

    private static final Logger logger = LoggerFactory.getLogger(RoomService.class);

    /** 房间列表 */
    private final static ConcurrentHashMap<String, Room> roomList = new ConcurrentHashMap<>();
    /** 用户房间 */
    private final static ConcurrentHashMap<User, Room> userRoom = new ConcurrentHashMap<>();

    @Autowired
    private GameService gameService;


    /**
     * 创建房间
     * @param channel
     * @param content
     * @return
     */
    public Message addRoom(Channel channel, Object content) {
        logger.info("处理创建房间");

        //创建房间
        Room room = new Room();
        room.setId(UUID.randomUUID().toString());
        room.setTime(60);
        roomList.put(room.getId(),room);

        logger.info("返回创建房间成功");
        return new Message(ResMessageType.addRoomSuccess,room);
    }

    /**
     * 进入房间
     * @param channel
     * @param content
     * @return
     */
    public Message inRoom(Channel channel, Object content) {
        logger.info("处理加入房间");

        //取出房间
        Map map = JSON.parseObject(String.valueOf(content), Map.class);
        String roomId = String.valueOf(map.get("roomId"));
        Room room = roomList.get(roomId);

        //取出用户
        User user = gameService.getUserByChannel(channel);

        //用户加入房间
        room.addUser(user);
        userRoom.put(user,room);

        //返回消息
        Message message = new Message(ResMessageType.inRoomSuccess,room);

        //通知其他用户加入成功
        for(User u : room.getUserList()){
            if(user==u||u.isRoot())continue;
            Channel ch = gameService.getChannelByUser(u);
            ch.writeAndFlush(message.toMsg());
        }

        //返回加入成功
        logger.info("返回加入房间成功");
        return message;
    }

    /**
     * 退出房间
     * @param channel
     * @param content
     * @return
     */
    public Message outRoom(Channel channel, Object content) {
        logger.info("处理退出房间");

        //取出房间
        Map map = JSON.parseObject(String.valueOf(content), Map.class);
        String roomId = String.valueOf(map.get("roomId"));
        Room room = roomList.get(roomId);

        //取出用户并删除
        User user = gameService.getUserByChannel(channel);
        room.removeUser(user);

        //判断是否空房间
        if(room.isEmpty()){
            roomList.remove(room.getId());
            room = null;
        }

        logger.info("返回退出房间");
        return new Message(ResMessageType.outRoom);
    }

    public Room getRoomByUser(User user) {
        return userRoom.get(user);
    }
}
